<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 梦幻西游数字资产管理系统</title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: url('/static/images/login.jpg') no-repeat center center fixed;
            background-size: cover;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Microsoft YaHei', 'SimHei', Arial, sans-serif;
            position: relative;
            overflow: hidden;
        }
        
        /* 背景动画云朵效果 */
        body::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: radial-gradient(ellipse at center, rgba(255,255,255,0.1) 0%, rgba(0,0,0,0.4) 100%);
            z-index: 0;
        }
        
        .login-container {
            width: 100%;
            max-width: 400px;
            padding: 20px;
            position: relative;
            z-index: 1;
            animation: fadeInUp 0.8s ease-out;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 游戏风格卡片 */
        .login-card {
            background: linear-gradient(135deg, 
                rgba(255, 245, 220, 0.95) 0%, 
                rgba(255, 250, 235, 0.92) 50%,
                rgba(255, 245, 220, 0.95) 100%);
            border-radius: 30px;
            box-shadow: 
                0 15px 50px rgba(255, 140, 0, 0.4),
                0 0 0 3px rgba(255, 200, 100, 0.8),
                inset 0 2px 10px rgba(255, 255, 255, 0.8);
            overflow: visible;
            position: relative;
            border: 4px solid #ff9933;
            backdrop-filter: blur(15px);
        }
        
        /* 装饰性边框 */
        .login-card::before {
            content: '';
            position: absolute;
            top: -10px;
            left: -10px;
            right: -10px;
            bottom: -10px;
            background: linear-gradient(45deg, #ffd700, #ff8c00, #ffd700);
            border-radius: 35px;
            z-index: -1;
            opacity: 0.3;
            filter: blur(10px);
        }
        
        /* Logo区域 */
        .login-header {
            padding: 20px 15px 15px;
            text-align: center;
            position: relative;
        }
        
        .logo-container {
            margin-bottom: 15px;
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% {
                transform: translateY(0px);
            }
            50% {
                transform: translateY(-8px);
            }
        }
        
        .logo-img {
            width: 220px;
            max-width: 85%;
            height: auto;
            filter: drop-shadow(0 5px 15px rgba(255, 140, 0, 0.5));
        }
        
        .welcome-text {
            font-size: 16px;
            font-weight: bold;
            color: #ff6600;
            text-shadow: 
                2px 2px 0 #fff,
                -1px -1px 0 #fff,
                1px -1px 0 #fff,
                -1px 1px 0 #fff,
                3px 3px 5px rgba(255, 100, 0, 0.3);
            margin-top: 8px;
            letter-spacing: 1.5px;
        }
        
        /* 登录表单区域 */
        .login-body {
            padding: 15px 28px 28px;
        }
        
        /* 游戏风格输入框 */
        .input-group-custom {
            margin-bottom: 16px;
            position: relative;
        }
        
        .input-label {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 13px;
            font-weight: bold;
            color: #8b4513;
            margin-bottom: 6px;
            text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.8);
        }
        
        .input-label i {
            font-size: 15px;
            color: #ff8c00;
        }
        
        .game-input {
            width: 100%;
            padding: 12px 16px;
            border: 3px solid #ff9933;
            border-radius: 12px;
            background: rgba(255, 255, 255, 0.85);
            backdrop-filter: blur(5px);
            font-size: 14px;
            color: #333;
            transition: all 0.3s ease;
            box-shadow: 
                inset 0 2px 5px rgba(0, 0, 0, 0.1),
                0 2px 8px rgba(255, 140, 0, 0.2);
        }
        
        .game-input:focus {
            outline: none;
            border-color: #ff6600;
            background: rgba(255, 255, 255, 0.95);
            box-shadow: 
                inset 0 2px 5px rgba(0, 0, 0, 0.1),
                0 0 0 4px rgba(255, 140, 0, 0.3),
                0 5px 15px rgba(255, 140, 0, 0.4);
            transform: translateY(-2px);
        }
        
        .game-input::placeholder {
            color: #999;
        }
        
        /* 记住我 */
        .remember-section {
            display: flex;
            align-items: center;
            margin-bottom: 16px;
        }
        
        .remember-checkbox {
            width: 16px;
            height: 16px;
            margin-right: 6px;
            cursor: pointer;
            accent-color: #ff8c00;
        }
        
        .remember-label {
            font-size: 13px;
            color: #8b4513;
            font-weight: 500;
            cursor: pointer;
            user-select: none;
        }
        
        /* 游戏风格按钮 */
        .btn-game-login {
            width: 100%;
            padding: 13px;
            border: none;
            border-radius: 16px;
            background: linear-gradient(135deg, #ff8c00 0%, #ff6600 100%);
            color: white;
            font-size: 16px;
            font-weight: bold;
            letter-spacing: 2px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            box-shadow: 
                0 5px 15px rgba(255, 100, 0, 0.4),
                inset 0 -3px 0 rgba(0, 0, 0, 0.2),
                inset 0 2px 0 rgba(255, 255, 255, 0.3);
            transition: all 0.3s ease;
            border: 3px solid #ff9933;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .btn-game-login::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
            transition: left 0.5s;
        }
        
        .btn-game-login:hover {
            transform: translateY(-3px);
            box-shadow: 
                0 8px 20px rgba(255, 100, 0, 0.5),
                inset 0 -3px 0 rgba(0, 0, 0, 0.2),
                inset 0 2px 0 rgba(255, 255, 255, 0.3);
        }
        
        .btn-game-login:hover::before {
            left: 100%;
        }
        
        .btn-game-login:active {
            transform: translateY(0);
            box-shadow: 
                0 3px 10px rgba(255, 100, 0, 0.3),
                inset 0 2px 5px rgba(0, 0, 0, 0.2);
        }
        
        /* 默认账号提示 */
        .default-account {
            margin-top: 16px;
            padding: 12px;
            background: rgba(255, 235, 205, 0.9);
            border-radius: 12px;
            border: 2px solid #ffcc99;
            font-size: 12px;
            color: #8b4513;
            text-align: center;
            box-shadow: 
                inset 0 2px 5px rgba(255, 200, 150, 0.5),
                0 2px 8px rgba(255, 140, 0, 0.2);
        }
        
        .default-account i {
            color: #ff8c00;
            margin-right: 5px;
        }
        
        .default-account strong {
            color: #ff6600;
            font-weight: bold;
        }
        
        /* 错误提示 */
        .alert-game {
            border-radius: 15px;
            border: 3px solid #ff4444;
            background: rgba(255, 230, 230, 0.95);
            backdrop-filter: blur(5px);
            color: #cc0000;
            padding: 15px;
            margin-bottom: 20px;
            font-weight: 500;
            box-shadow: 0 3px 10px rgba(255, 0, 0, 0.2);
            animation: shake 0.5s;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }
        
        /* 装饰性元素 */
        .deco-corner {
            position: absolute;
            width: 32px;
            height: 32px;
            background: radial-gradient(circle, #ffd700, #ff8c00);
            opacity: 0.6;
            border-radius: 50%;
        }
        
        .deco-corner.top-left {
            top: -8px;
            left: -8px;
            box-shadow: 0 0 16px rgba(255, 215, 0, 0.6);
        }
        
        .deco-corner.top-right {
            top: -8px;
            right: -8px;
            box-shadow: 0 0 16px rgba(255, 215, 0, 0.6);
        }
        
        .deco-corner.bottom-left {
            bottom: -8px;
            left: -8px;
            box-shadow: 0 0 16px rgba(255, 215, 0, 0.6);
        }
        
        .deco-corner.bottom-right {
            bottom: -8px;
            right: -8px;
            box-shadow: 0 0 16px rgba(255, 215, 0, 0.6);
        }
        
        /* 响应式设计 */
        @media (max-width: 576px) {
            .login-container {
                padding: 15px;
                max-width: 95%;
            }
            
            .login-body {
                padding: 15px 20px 22px;
            }
            
            .logo-img {
                width: 180px;
            }
            
            .welcome-text {
                font-size: 14px;
            }
            
            .btn-game-login {
                font-size: 15px;
                padding: 12px;
                letter-spacing: 1.5px;
            }
            
            .game-input {
                padding: 11px 14px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <!-- 装饰角 -->
            <div class="deco-corner top-left"></div>
            <div class="deco-corner top-right"></div>
            <div class="deco-corner bottom-left"></div>
            <div class="deco-corner bottom-right"></div>
            
            <!-- Logo头部 -->
            <div class="login-header">
                <div class="logo-container">
                    <img src="/static/images/mh_logo.png" alt="梦幻西游" class="logo-img">
                </div>
                <div class="welcome-text">数字资产管理系统</div>
            </div>
            
            <!-- 登录表单 -->
            <div class="login-body">
                <div id="errorMessage" class="alert-game" style="display: none;">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                    <span id="errorText"></span>
                </div>
                
                <form id="loginForm" onsubmit="return handleLogin(event)">
                    <div class="input-group-custom">
                        <div class="input-label">
                            <i class="bi bi-person-circle"></i>
                            <span>账号</span>
                        </div>
                        <input 
                            type="text" 
                            class="game-input" 
                            id="username" 
                            placeholder="请输入账号" 
                            required 
                            autofocus
                        >
                    </div>
                    
                    <div class="input-group-custom">
                        <div class="input-label">
                            <i class="bi bi-shield-lock"></i>
                            <span>密码</span>
                        </div>
                        <input 
                            type="password" 
                            class="game-input" 
                            id="password" 
                            placeholder="请输入密码" 
                            required
                        >
                    </div>
                    
                    <div class="remember-section">
                        <input type="checkbox" class="remember-checkbox" id="rememberMe">
                        <label class="remember-label" for="rememberMe">记住账号</label>
                    </div>
                    
                    <button type="submit" class="btn-game-login">
                        <i class="bi bi-box-arrow-in-right me-2"></i>进入游戏
                    </button>
                </form>
                
                <div class="default-account">
                    <i class="bi bi-info-circle-fill"></i>
                    默认账号：<strong>admin</strong> / 密码：<strong>admin123</strong>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 防止重复检查的标记
        let isChecking = false;
        
        // 检查是否已登录（验证服务器端session）
        async function checkLoginStatus() {
            // 防止重复调用
            if (isChecking) {
                return;
            }
            
            if (localStorage.getItem('isLoggedIn') === 'true') {
                isChecking = true;
                try {
                    // 使用专门的登录验证接口，避免使用/api/config
                    const response = await fetch('/api/check-session', {
                        method: 'GET',
                        credentials: 'same-origin'  // 确保发送cookies
                    });
                    
                    if (response.ok) {
                        const result = await response.json();
                        if (result.code === 200 && result.logged_in) {
                            // Session有效，跳转到首页
                            window.location.href = '/';
                            return;
                        }
                    }
                    
                    // Session无效，清除本地登录状态
                    localStorage.removeItem('isLoggedIn');
                    localStorage.removeItem('username');
                } catch (error) {
                    // 网络错误或session无效，清除本地登录状态
                    console.error('Session check error:', error);
                    localStorage.removeItem('isLoggedIn');
                    localStorage.removeItem('username');
                } finally {
                    isChecking = false;
                }
            }
        }
        
        // 页面加载时检查登录状态
        // 使用setTimeout避免在Edge中立即执行导致的竞态条件
        setTimeout(() => {
            checkLoginStatus();
        }, 100);
        
        // 加载记住的用户名
        const rememberedUsername = localStorage.getItem('rememberedUsername');
        if (rememberedUsername) {
            document.getElementById('username').value = rememberedUsername;
            document.getElementById('rememberMe').checked = true;
        }
        
        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            const errorText = document.getElementById('errorText');
            errorText.textContent = message;
            errorDiv.style.display = 'block';
            
            setTimeout(() => {
                errorDiv.style.display = 'none';
            }, 3000);
        }
        
        async function handleLogin(event) {
            event.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const rememberMe = document.getElementById('rememberMe').checked;
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    // 登录成功
                    localStorage.setItem('isLoggedIn', 'true');
                    localStorage.setItem('username', username);
                    
                    // 记住用户名
                    if (rememberMe) {
                        localStorage.setItem('rememberedUsername', username);
                    } else {
                        localStorage.removeItem('rememberedUsername');
                    }
                    
                    // 跳转到首页
                    window.location.href = '/';
                } else {
                    showError(result.message || '登录失败，请重试');
                }
            } catch (error) {
                showError('网络错误，请检查连接');
                console.error('Login error:', error);
            }
            
            return false;
        }
        
        // 回车登录
        document.getElementById('password').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                handleLogin(e);
            }
        });
    </script>
</body>
</html>
